﻿//--------------------------------------------------------------------
// 动态报表页眉控件 v1.00
//
// 开发人： 朱明智
// 创建时间： 2019-08-22
//
// 修改人：
// 修改时间：
//--------------------------------------------------------------------

using System.Collections.Generic;
using System.Drawing;
using System.Threading;

namespace Dynamic.Controller
{
    /// <summary>
    /// 页眉类
    /// </summary>
    public class PageHeader : ReportController
    {
        /// <summary>
        /// 标题
        /// </summary>
        private TextBox _caption;

        private List<Row> _rectangles = new List<Row>();

        /// <summary>
        /// 容器中控件高度综合
        /// </summary>
        private float _TextBoxHeight
        {
            get
            {
                float hight = _caption == null ? 0f : _caption.Height;

                foreach (var item in _rectangles) hight += item.Height;

                return hight;
            }
        }

        public float Height
        {
            get { return base.height; }
            private set { base.height = value; }
        }

        public PageHeader()
        {
            template = TemplateHelper.PageHeaderTemp;
        }

        public void AddCaption(string text)
        {
            AddCaption(text, 1f, new Font("黑体", 16f, FontStyle.Bold), Color.Black);
        }

        public void AddCaption(string text, float height, Font font, Color color)
        {
            _caption = new TextBox(text, height, TextAlign.Center, VerticalAlign.Middle)
            {
                Font = font,
                Color = color
            };
            _caption.Parent = this;
        }

        /// <summary>
        /// 添加矩形容器
        /// </summary>
        public void AddRectangle(Row rectangle)
        {
            _rectangles.Add(rectangle);
            rectangle.Parent = this;
        }

        // 计算修正位置Caption & Rectangles
        private void CalculationPosition()
        {
            // 修正页眉高度
            if (Height < _TextBoxHeight) Height = _TextBoxHeight;

            float top = _caption.Height + _caption.Top;
            for (int i = 0; i < _rectangles.Count; i++)
            {
                var rectangle = _rectangles[i];
                rectangle.Top = top;
                top += rectangle.Height;
            }
        }

        public override string ToString()
        {
            CalculationPosition();
            var str = base.ToString();
            str = str.Replace("@Caption", _caption != null ? _caption.GetTempStr() : "");

            for (int i = 0; i < _rectangles.Count; i++)
            {
                Row rectangle = _rectangles[i];
                // 最后一个
                str = i == _rectangles.Count - 1
                    ? str.Replace("@Rectangle", rectangle.GetTempStr())
                    : str.Insert(str.IndexOf("@Rectangle"), rectangle.GetTempStr());
            }

            if (_caption == null && _rectangles.Count == 0)
                str = str.Replace("<ReportItems></ReportItems>", "");
            return str;
        }

      
    }
}